package com.tbit.uqbike.client.service.impl;

import com.tbit.uqbike.center.pojo.MachineStatus;
import com.tbit.uqbike.client.constant.AccountConfigExtConstant;
import com.tbit.uqbike.client.constant.MapConstant;
import com.tbit.uqbike.client.constant.SysConstant;
import com.tbit.uqbike.client.dao.core.MachineBorrowDao;
import com.tbit.uqbike.client.dto.BorrowUserInfoDTO;
import com.tbit.uqbike.client.exception.BaseException;
import com.tbit.uqbike.client.pojo.Machine;
import com.tbit.uqbike.client.pojo.MachineBorrow;
import com.tbit.uqbike.client.pojo.Point;
import com.tbit.uqbike.client.pojo.newEntity.AccountConfigExt;
import com.tbit.uqbike.client.service.*;
import com.tbit.uqbike.client.util.GeoUtil;
import com.tbit.uqbike.client.util.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;

@Service("machineBorrowService")
public class MachineBorrowServiceImpl implements MachineBorrowService {
    @Autowired
    private MachineBorrowDao machineBorrowDao;
    @Resource
    private MachineStatusService machineStatusService;
    @Resource
    private CalibrateService calibrateService;
    @Resource
    private CacheService cacheService;


    @Override
    public MachineBorrow getByUserId(Integer userId) {
        return machineBorrowDao.getByUserId(userId);
    }

    @Override
    public MachineBorrow getByMachineId(Integer machineId) {
        return machineBorrowDao.getByMachineId(machineId);
    }

    @Override
    public List<MachineBorrow> getByMachineIdBatch(List<Integer> machineIds) {
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("machineIds", StringUtil.getListString(machineIds));

        return machineBorrowDao.getByMachineIdBatch(params);
    }

    @Override
    public void add(MachineBorrow machineBorrow) {
        machineBorrowDao.insert(machineBorrow);
    }

    @Override
    public void del(Integer machineId) {
        machineBorrowDao.del(machineId);
    }

    @Override
    public List<Integer> getAll() {
        return machineBorrowDao.getAll();
    }

    @Override
    public MachineBorrow getByOrderNo(String orderNO) {
        return machineBorrowDao.getByOrderNo(orderNO);
    }

    @Override
    public boolean validOutOfRange(BorrowUserInfoDTO borrowUserInfoDTO, Machine machine) {
        MachineStatus machineStatus = machineStatusService.getByMachineNO(machine.getMachineNO(), MapConstant.MAP_GOOGLE);
        if (Objects.isNull(machineStatus) || Objects.isNull(machineStatus.getLon()) || Objects.isNull(machineStatus.getLat())){
            throw new BaseException("-3029", "故障车辆，车辆还没有投放");
        }
        AccountConfigExt accountConfigExtByKey = cacheService.getAccountConfigExtByKey(machineStatus.getAccountId(), AccountConfigExtConstant.OUT_OF_RANGE);
        return !machineStatus.getInGeo() && Objects.nonNull(accountConfigExtByKey) && Objects.equals(accountConfigExtByKey.getParamValue(),"1");
    }

    @Override
    public boolean validTooFar(BorrowUserInfoDTO borrowUserInfoDTO, Machine machine) {
        MachineStatus machineStatus = machineStatusService.getByMachineNO(machine.getMachineNO(), MapConstant.MAP_GOOGLE);
        if (Objects.isNull(machineStatus) || Objects.isNull(machineStatus.getLon()) || Objects.isNull(machineStatus.getLat())){
            throw new BaseException("-3029", "故障车辆，车辆还没有投放");
        }
        Point machinePoint = new Point(machineStatus.getLon(), machineStatus.getLat());
        Point userPoint = new Point(borrowUserInfoDTO.getLon(), borrowUserInfoDTO.getLat());
        userPoint = calibrateService.calibrate(userPoint, borrowUserInfoDTO.getMapType(), SysConstant.ORIGINAL);
        double distance = GeoUtil.getPointDistance(userPoint, machinePoint);
        AccountConfigExt ext = cacheService.getAccountConfigExtByKey(machineStatus.getAccountId()
                , AccountConfigExtConstant.ACCOUNTCONFIG_MACHINE_BORROW_RANGE);
        if (Objects.nonNull(ext)){
            int value = Integer.parseInt(ext.getParamValue());
            if (distance < value || value <= 0) {
                return false;
            }
        }
        AccountConfigExt accountConfigExtByKey = cacheService.getAccountConfigExtByKey(machineStatus.getAccountId(), AccountConfigExtConstant.TOO_FAR_BORROW);
        if (Objects.nonNull(accountConfigExtByKey) && Objects.equals(accountConfigExtByKey.getParamValue(),"1")){
                return true;
        }
        return false;
    }
}